package com.admin.service;

import com.admin.dao.DeliveryDao;
import com.admin.entity.Delivery;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.utils.admin.ObjectUtils;
import com.admin.sys.base.module.extend.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

//出库信息接口类
@Service
public class DeliveryService extends BaseService {

    @Autowired
    private DeliveryDao deliveryDao;

    //【出库信息】设置查询条件
    private LambdaQueryWrapper<Delivery> getDeliveryQueryCondition(Delivery delivery) {
        LambdaQueryWrapper<Delivery> lambdaQuery = this.getBaseQueryCondition(delivery);
        //根据创建时间排序
        lambdaQuery.orderByDesc(Delivery::getCreateDate);
        if (ObjectUtils.isNotNull(delivery)) {
            //【主键id】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(delivery.getId()), Delivery::getId, delivery.getId());
            //【供应商名称】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(delivery.getSupplierId()), Delivery::getSupplierId, delivery.getSupplierId());
            //【物品名称】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(delivery.getGoodsId()), Delivery::getGoodsId, delivery.getGoodsId());
            //【出库数量】模糊查询
            lambdaQuery.like(StringUtils.isNotNull(delivery.getNumber()), Delivery::getNumber, delivery.getNumber());
            //【出库时间】精确查询
            lambdaQuery.eq(ObjectUtils.isNotNull(delivery.getDeliveryTime()), Delivery::getDeliveryTime, delivery.getDeliveryTime());
            //【出库时间】范围查询
            if (ObjectUtils.isNotNull(delivery.getDeliveryTimeRange()) && delivery.getDeliveryTimeRange().size() > 0) {
                lambdaQuery.between(Delivery::getDeliveryTime, delivery.getDeliveryTimeRange().get(0), delivery.getDeliveryTimeRange().get(1));
            }
            //【操作人】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(delivery.getUserId()), Delivery::getUserId, delivery.getUserId());
        }
        return lambdaQuery;
    }

    //【出库信息】分页查询
    public IPage<Delivery> getPage(Page<Delivery> page, Delivery delivery) {
        LambdaQueryWrapper<Delivery> lambdaQuery = getDeliveryQueryCondition(delivery);
        return deliveryDao.selectPage(page, lambdaQuery);
    }
    
    //【出库信息】查询列表
    public List<Delivery> getList(Delivery delivery) {
        LambdaQueryWrapper<Delivery> lambdaQuery = getDeliveryQueryCondition(delivery);
        return deliveryDao.selectList(lambdaQuery);
    }
    
    //【出库信息】根据id查询
    public Delivery get(String id) {
        return deliveryDao.selectById(id);
    }

    //【出库信息】根据对象查询
    public Delivery get(Delivery delivery) {
    LambdaQueryWrapper<Delivery> lambdaQuery = getDeliveryQueryCondition(delivery);
        return deliveryDao.selectOne(lambdaQuery);
    }
    
    //【出库信息】新增
    public int insert(Delivery delivery) {
        this.preInsert(delivery);
        return deliveryDao.insert(delivery);
    }
    
    //【出库信息】修改
    public int update(Delivery delivery) {
        this.preUpdate(delivery);
        return deliveryDao.updateById(delivery);
    }
    
    //【出库信息】删除
    public int delete(String id) {
        return deliveryDao.deleteById(id);
    }

    //【出库信息】批量删除
    public int delAll(String[] ids) {
        return deliveryDao.deleteBatchIds(Arrays.asList(ids));
    }

}